/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package taglibs;

import daos.ItemDAO;
import java.util.ArrayList;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import models.Item;
import util.WebConst;

/**
 *
 * @author trietto
 */
public class SearchResult extends TagSupport {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /*
     * (non-Javadoc)
     * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
     */
    public int doStartTag() {
        try {
            JspWriter writer = pageContext.getOut();
            ArrayList<Item> lstItems = new ArrayList<Item>();
            String sCategoryId = pageContext.getRequest().getParameter("cbCategory");
            String name = pageContext.getRequest().getParameter("txtName");

            if(sCategoryId==null||name==null){
                return SKIP_BODY;
            }
            int categoryId = Integer.parseInt(sCategoryId);
            int totalRecord = ItemDAO.singleton().searchCount(categoryId, name);
            int currentPage = 0;
            try {
                currentPage = Integer.parseInt(pageContext.getRequest().getParameter(WebConst.PAGE));
            } catch (Exception e) {
                currentPage = 0;
            }
            //set current page
            currentPage = (currentPage == 0 ? 1 : currentPage);
            lstItems = ItemDAO.singleton().search(categoryId, name, currentPage);

            writer.println("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
            writer.println("<tr>");
            writer.println("<td align='center'><b>SEARCH RESULT</b><br></td>");
            writer.println("</tr>");

            Item item1 = null;
            Item item2 = null;
            for (int i = 0; i <= lstItems.size()-1; i++) {
                item1 = lstItems.get(i);
                writer.println("<tr>");
                writer.println("<td>");
                writer.println("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
                writer.println("<tr>");
                writer.println("<td><a href='BookDetail.jsp?itemid="+item1.getItemId()+"'>"
                        + item1.getName());
                writer.println("</a></td>");
                writer.println("</tr>");

                writer.println("<tr>");
                writer.println("<td>");
                writer.println("Author: " + item1.getAuthor());
                writer.println("</td>");
                writer.println("</tr>");

                
                writer.println("<tr>");
                writer.println("<td>");
                writer.println("<img alt='' src='"+item1.getImageURL()+"' />");
                writer.println("</td>");
                writer.println("</tr>");

                writer.println("<tr>");
                writer.println("<td>Price:"+item1.getPrice());
                writer.println("</td>");
                writer.println("</tr>");

                writer.println("<tr>");
                writer.println("<td><a href='ShoppingCartServlet?itemid="+item1.getItemId()
                        +"'>Add to Cart</a>");
                writer.println("</td>");
                writer.println("</tr>");

                writer.println("</table>");
                writer.println("</td>");
                i++;
                if(i>=lstItems.size()) break;
                item2 = lstItems.get(i);
                writer.println("<td>");
                writer.println("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
                writer.println("<tr>");
                writer.println("<td><a href='BookDetail.jsp?itemid="+item2.getItemId()+"'>"
                        + item2.getName());
                writer.println("</a></td>");
                writer.println("</tr>");

                writer.println("<tr>");
                writer.println("<td>");
                writer.println("Author: " + item2.getAuthor());
                writer.println("</td>");
                writer.println("</tr>");


                writer.println("<tr>");
                writer.println("<td>");
                writer.println("<img alt='' src='"+item2.getImageURL()+"' />");
                writer.println("</td>");
                writer.println("</tr>");

                writer.println("<tr>");
                writer.println("<td>Price:"+item2.getPrice());
                writer.println("</td>");
                writer.println("</tr>");

                writer.println("<tr>");
                writer.println("<td><a href='ShoppingCartServlet?itemid="+item2.getItemId()
                        +"'>Add to Cart</a>");
                writer.println("</td>");
                writer.println("</tr>");

                writer.println("</table>");
                writer.println("</td>");
                writer.println("</tr>");
            }

            writer.println("<tr>");
            writer.println("<td colspan='2'>");
            //print paging
            int numPage = totalRecord / WebConst.ROW_PER_PAGE;
            numPage = (totalRecord % WebConst.ROW_PER_PAGE>0)?numPage+1:numPage;
            if (numPage > 1) {
                int p = 0;
                for (int i = 0; i < numPage; i++) {
                    p=i+1;
                    if (currentPage == p) {
                        writer.print("&nbsp;" + p);
                    } else {
                        writer.print("&nbsp;"
                                + "<a href='BookSearch.jsp?action=view"
                                + "&page=" + p + "'>" + p + "</a>");
                    }
                }
            }
            writer.println("</td>");
            writer.println("</tr>");
            writer.println("</table>");
        } catch (Exception e) {
        }
        return SKIP_BODY;
    }
}
